package com.example.pinkcat.activity.pinkCatUser;

import android.content.Context;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Save_set_Msg{
    public static void Save(UserMsg entity,Context context){
        String filepath=context.getFilesDir()+"/pinkcat_user/";
        File file=new File(filepath);
        if (!file.exists()){
            file.mkdir();
        }
        try {
            BufferedWriter writer=new BufferedWriter(new FileWriter(filepath+"entity.pinkcat"));
            Gson gson=new Gson();
            String data=gson.toJson(entity);
            System.out.println(data);
            writer.write(data);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写入文件失败");
        }
    }

    public static void setMsg(Context context){
        String filepath=context.getFilesDir()+"/pinkcat_user/";
        File file=new File(filepath);
        if (!file.exists()){
            file.mkdir();
        }
        try {
            BufferedReader reader=new BufferedReader(new FileReader(filepath+"entity.pinkcat"));
            String data=reader.readLine();
            System.out.println(data);
            Gson gson=new Gson();
            UserMsg entity=gson.fromJson(data,UserMsg.class);
            userStaticMsg.userId= entity.userId;//-1
            userStaticMsg.userName=entity.userName;
            userStaticMsg.phone=entity.phone;
            userStaticMsg.mail=entity.mail;
            userStaticMsg.head_img =entity.head_img;
            userStaticMsg.login=entity.login;
            userStaticMsg.vip=entity.vip;
            userStaticMsg.style=entity.style;
            userStaticMsg.appVersion=entity.appVersion;
            userStaticMsg.appUrl=entity.appUrl;
            userStaticMsg.JSESSIONID=entity.JSESSIONID;
            userStaticMsg.password=entity.password;
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写入文件失败");
        }
    }
}
